package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private  SpuService spuService;
    @Autowired
    private  SpuDetailService detailService;
    @Autowired
    private  SkuService skuService;



    /**
     * 分页查询spu
     * @param brandId
     * @param categoryId
     * @param id
     * @param page
     * @param rows
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>>querySpuByPage(
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){
        return ResponseEntity.ok(spuService.querySpuByPage(brandId,categoryId,id,page,rows,saleable));

    }

    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品上下架
     * 商品修改前，必须先下架，因此要完成商品的上架和下架功能
     * SPU下架时，其关联的SKU也必须保持下架状态，
     * 因此这里业务中不仅更新了SPU表，也更新了SKU表。
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void>updateSpuSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable){
        spuService.updateSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //根据id查询spu及sku、spuDetail等

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO>queryGoodsById(@PathVariable("id")Long id){
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }

    /**
     * 更新商品
     * @param spuDTO
     * @return
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询SpuDetail
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id){
        return ResponseEntity.ok(detailService.getDetailById(id));
    }

    /**
     * 根据SpuId查询SKU集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long id){
        return ResponseEntity.ok(this.skuService.getSkuBySpuId(id));
    }


    /**
     * 根据id集合查询SKU集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>>querySkuBySpuId(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok((skuService.getSkuBySpuIds(ids)));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam(value = "id")Long id,
            @RequestParam(value = "searching",required = false) Boolean searching) {
        List<SpecParamDTO> specParamDTOS = detailService.querySpecValues(id, searching);
        return ResponseEntity.ok(specParamDTOS);
    }
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){

        return ResponseEntity.ok(spuService.querySpuById(id));
    }

}
